package com.example;

import java.io.FileWriter;
import java.io.IOException;
import java.sql.Timestamp;
import java.time.LocalDateTime;
import java.util.Random;

public class DataGenerator {
    private static final String OUTPUT_FILE = "data/user_purchases.csv";
    private static final int NUM_USERS = 100;
    private static final int PURCHASES_PER_USER = 20;
    private static final Random random = new Random();

    public static void main(String[] args) {
        try (FileWriter writer = new FileWriter(OUTPUT_FILE)) {
            // 生成用户购买数据
            for (int userId = 1; userId <= NUM_USERS; userId++) {
                generateUserPurchases(writer, "user" + userId);
            }
            System.out.println("数据生成完成，保存到: " + OUTPUT_FILE);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private static void generateUserPurchases(FileWriter writer, String userId) throws IOException {
        LocalDateTime startTime = LocalDateTime.now().minusDays(90);
        
        for (int i = 0; i < PURCHASES_PER_USER; i++) {
            // 随机生成订单金额
            double amount = 10 + random.nextDouble() * 490; // 10-500元
            
            // 随机生成购买时间（在过去90天内）
            LocalDateTime purchaseTime = startTime.plusSeconds(random.nextInt(90 * 24 * 3600));
            
            // 生成订单ID
            String orderId = "order" + System.currentTimeMillis() + random.nextInt(1000);
            
            // 写入CSV文件
            writer.write(String.format("%s,%s,%.2f,%s\n", 
                userId, orderId, amount, Timestamp.valueOf(purchaseTime)));
        }
    }
}